<?php
/**
 * Contact Statuses List Grid
 * This is the administration interface to add/update/delete 
 * contact status.
 *
 * @author Xavier VILAIN
 * @package Hive\Ressource
 * @license #license-hive#
 *  
 */
use Fox\Generator;
use Fox\Dao\SqlFragment;
$xview = Xview::instance();
// Customizing
$customizing = Hive\Admin\Customizing::instance();
$customizing->trackSummaryPage(array('mod' =>'contacts','act'=>'statuses','sac' => null));

if(!Fox\Authorization\Manager::check(Hive\Authorizations::CONTACT_STATUSES_W)) {
	echo $xview->accessDenied(Msg(Hive::BUNDLE,Hive::ERR_ADMIN_ACCES_DENIED),null,true);
	exit;
}

$reload = $xview->val('reload',false);
$screen = new Fox\Html\Screen('sStatuses');
$screen->pageTitle(Msg(Hive::BUNDLE,Hive::CONTACT_STATUS_LIST));
// Type List (For help)
$helpMessage = '<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::YOU_WILL_FIND_BELLOW_THE_LIST_OF_AVAILABLE_STATUS_FOR_YOUR_CONTACTS).'</p>';
$gStatusType = new Fox\Grid('statusType');
$gStatusType->fieldcat(array('label','description'));
$gStatusType->bootstrapGrid()
			->fieldcat()->switchToObjectMode();
$gStatusType->fieldcat()->setGenerators(array(
		'label' => Generator::call('label'),
		'description' => Generator::call('description'),
	));
$typesLabel = array();
$data = \Hive\Contact\Status\Type::loadActives();
foreach($data as $item ) {
	$typesLabel[$item->type] = $item->label();
}
$gStatusType->initialize($data);
$helpMessage .= $gStatusType->display();
$screen->message($helpMessage);

// Status List
$gStatus = new Fox\Grid('status');
$gStatus->fieldcat(array('label',array('contact_status_type','type'),'standard','active','iedit'))
			->switchToObjectMode()
			->setGenerators(array(
				'label' => Generator::call('label'),
				'type' => Generator::useFunction(function($data) use($typesLabel){
			
					if(isset($typesLabel[$data->type])) {
						return $typesLabel[$data->type];
					}
				}),
				'iedit' => Generator::useValue('?')
			));
$gStatus->fieldcat()->field('iedit')->renderer()
		->behaviors(Fox\Behaviors::action(
			Generator::url('contacts','statuses','modify',array('inlineid' => Generator::call('GetInLineId'))),
			true
		));
$gStatus->bootstrapGrid();
$gStatus->footer()->add(new Fox\Html\Button('add',null,Msg(Hive::BUNDLE,Hive::ADD_A_NEW_STATUS)))
		->color( Fox\Html\Button::COLOR_WARNING)
		->behaviors(Fox\Behaviors::action(
			Generator::url('contacts','statuses','modify'),
			true
		));
$gStatus->initialize(Hive\Contact\Status::loadAll());
$screen->add($gStatus);
$html = $screen->display();

return '<div class="source {\'uri\':\'?mod=contacts&act=statuses\'}" >'.$html.'</div>';